package hu.webdream.android.contacts;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class ContactDbHelper extends SQLiteOpenHelper implements BaseColumns {

	public static final String DB_NAME = "contacts.db";
	public static final String TABLE_NAME = "contacts";
	public static final String NAME = "name";
	public static final String PHONE = "phone";

	public ContactDbHelper(Context context) {
		super(context, ContactDbHelper.DB_NAME, null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + ContactDbHelper.TABLE_NAME + " ("
				+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
				+ ContactDbHelper.NAME + " TEXT, " + ContactDbHelper.PHONE
				+ " TEXT);");

		this.loadDemoData(db);
	}

	public void loadDemoData(SQLiteDatabase db) {
		ContentValues values = new ContentValues();

		values.put(ContactDbHelper.NAME, "Tom");
		values.put(ContactDbHelper.PHONE, "06302971659");
		db.insert(ContactDbHelper.TABLE_NAME, ContactDbHelper.NAME, values);

		values.put(ContactDbHelper.NAME, "John");
		values.put(ContactDbHelper.PHONE, "06302525633");
		db.insert(ContactDbHelper.TABLE_NAME, ContactDbHelper.NAME, values);

		values.put(ContactDbHelper.NAME, "Bob");
		values.put(ContactDbHelper.PHONE, "06112345678");
		db.insert(ContactDbHelper.TABLE_NAME, ContactDbHelper.NAME, values);

		values.put(ContactDbHelper.NAME, "Joe");
		values.put(ContactDbHelper.PHONE, "066060600606");
		db.insert(ContactDbHelper.TABLE_NAME, ContactDbHelper.NAME, values);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + ContactDbHelper.TABLE_NAME);
		this.onCreate(db);
	}

}
